package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.Collect;
import com.dropdb.demo.bean.CollectExample;
import com.dropdb.demo.bean.bo.BaseData;
import com.dropdb.demo.mapper.CollectMapper;
import com.dropdb.demo.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:tanxin Desc: 会员收藏
 * @date:2020/6/26 20:38
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Resource
    CollectMapper collectMapper;
    @Override
    public BaseData queryCollection(Integer page, Integer limit, String sort, String order,Integer userId,Integer valueId) {
        CollectExample collectExample = new CollectExample();
        //条件查询
        if (userId != null && valueId != null) {
           collectExample.createCriteria().andUserIdEqualTo(userId).andValueIdEqualTo(valueId);
        }else if (userId != null) {
           collectExample.createCriteria().andUserIdEqualTo(userId);
        }else if (valueId != null) {
           collectExample.createCriteria().andValueIdEqualTo(valueId);
        }
        collectExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page,limit);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<Collect> pageInfo = new PageInfo<>(collects);
        long total = pageInfo.getTotal();
        return new BaseData(total,collects);
    }
}
